package cn.kp.learning.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.kp.learning.model.CorePermission;
import cn.kp.learning.model.CorePermissionExample;
import cn.kp.learning.service.CorePermissionService;
import cn.kp.learning.util.ResponseUtil;

@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Resource
	private CorePermissionService corePermissionService;
	
	@RequestMapping("/getPermissionList")
	public void getPermissionList(HttpServletResponse response,HttpServletRequest request) throws Exception{
		System.out.println("system permission get " );
		CorePermissionExample example = new CorePermissionExample();
		
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));
		
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			example.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			example.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		
		List<CorePermission> lt = corePermissionService.getPermissionList(example);
		int countPermission = corePermissionService.countPermission(example);
		
		JSONObject json = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		//json.put("pages", lt.size()/10+1);
		json.put("total", countPermission);
		json.put("rows", jsonArray);
		
		System.out.println("system permission Json : " + json.toString());
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
